package com.example.androidroomdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.androidroomdemo.Dao.UserDao;
import com.example.androidroomdemo.Entities.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * https://huaweicloud.csdn.net/63357767d3efff3090b57eeb.html?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Eactivity-3-124723516-blog-126385379.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Eactivity-3-124723516-blog-126385379.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=6
 */
public class MainActivity extends AppCompatActivity {


    @BindView(R.id.rv_user)
    RecyclerView rvUser;
    List<User> userList = new ArrayList<>();
    UserDao userDao;
    private String TagL = MainActivity.class.getSimpleName();
    UserAdapter userAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        userDao = DbManager.getInstance().userDao();
        userAdapter = new UserAdapter(R.layout.activity_main_item, userList);
        rvUser.setLayoutManager(new LinearLayoutManager(this));
        rvUser.setAdapter(userAdapter);
      /*  delete();*/
        insertAll();
        userAdapter.addChildClickViewIds(R.id.item_update_tv, R.id.item_delete_tv);
        userAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                int id = view.getId();
                Log.e(TagL, id + "==" + position);
                 switch (id) {
                    case R.id.item_update_tv:
                        singleModify(userList.get(position));
                        break;
                    case R.id.item_delete_tv:
                        singleDel(userList.get(position));
                        break;
                }
            }
        });
    }

    @OnClick({R.id.tv_query, R.id.tv_insert, R.id.tv_update, R.id.tv_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_query:
                insertAll();
                break;
            case R.id.tv_insert:
                insertSingleData();
                break;
            case R.id.tv_update:
                update();
                break;
            case R.id.tv_delete:
                delete();
                break;
        }
    }


    /**
     * 查询
     */
    private void query() {
        userList = userDao.queryAllUser();
        Log.e(TagL, userList.toString());
        userAdapter.setList(userList);
    }

    /**
     * 默认插入批量数据
     */
    private void insertAll() {
        if (userList.size() <= 0) {
            List<User> mutableList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                User user = new User("提莫" + i, 20 + i, "贵阳市观山湖区", "");
                mutableList.add(user);
            }
            userDao.addBatchUser(mutableList);
        }
        query();
    }

    /**
     * 插入单条数据
     */
    private void insertSingleData() {
        Integer age = new Random().nextInt(20);
        User user = new User("盖伦", age, "德玛西亚", "");
        userDao.addUser(user);
        query();
    }

    /**
     * 删除表里的所有数据
     */
    private void delete() {
        userDao.deleteAllUser();
        query();
    }

    /**
     * 更新所有数据
     */
    private void update() {
        userDao.updateAll();
        query();
    }

    /**
     * 删除loginUser表里的指定删除某一个
     */
    private void singleDel(User singleUser) {
        userDao.deleteSingle(singleUser);

        query();
    }

    /**
     * 修改数据表里某一个对象的字段值
     */
    private void singleModify(User user) {
        user.setAliasName("修改的" + user.getAliasName());
        user.setAge(1000);
        user.setAds("修改的地址 德玛西亚");
        userDao.updateUser(user);
        userList.clear();
        query();
    }
}